package br.inf.solutions.apresentacoes.ssp.control.validator;

import java.util.Date;
import java.util.Map;

import br.com.vexillum.control.validator.Validator;
import br.com.vexillum.util.Return;
import br.inf.solutions.apresentacoes.ssp.model.PostGraduation;
import br.inf.solutions.apresentacoes.ssp.model.ReasonTransferReserve;
import br.inf.solutions.apresentacoes.ssp.model.Square;
import br.inf.solutions.apresentacoes.ssp.model.SquarePostReason;

public class ReasonTransferReserveValidator extends Validator {

	public ReasonTransferReserveValidator(Map<String, Object> mapData) {
		super(mapData);
	}
	
	

	@Override
	public Return validateSave() {
		Return ret = super.validateSave();
		if(ret.isValid()){
			ret.concat(enactmentOfLaw());
		}
		return ret;
	}
	
	@Override
	public Return validateUpdate() {
		Return ret = super.validateUpdate();
		if(ret.isValid()){
			ret.concat(enactmentOfLaw());
		}
		return ret;
	}
	
	public Return validateAddSquarePostReason(){
		Return ret = new Return(true);
		ReasonTransferReserve reason = (ReasonTransferReserve) entity;
		
		Square square = (Square) mapData.get("square");
		PostGraduation post = (PostGraduation) mapData.get("post");
		
		for(SquarePostReason s : reason.getSquarePostReason()){
			if(s.getSquare().equals(square) && s.getPost().equals(post)){
				ret.concat(creatReturn("post", getValidationMessage(null, "validateaddsquarepostreason", false)));
			}
		}
		return ret;
	}
	
	private Return enactmentOfLaw(){
		Return ret = new Return(true);
		ReasonTransferReserve reason = (ReasonTransferReserve) entity;
		Date startDate = reason.getInitialDateLaw();
		Date finalDate = reason.getFinalDateLaw();
		if(finalDate != null){
			if(finalDate.before(startDate)){
				ret.concat(creatReturn("finalDate", getValidationMessage(null, "enactmentoflawvalid", false)));
			}
		}
		return ret;
	}
	
	
}
